package com.wlyuan.open.domain.source.service.impl;

import com.alibaba.fastjson.JSON;
import com.wlyuan.core.exception.AssertUtils;
import com.wlyuan.open.domain.source.assembler.UserAssembler;
import com.wlyuan.open.domain.source.service.IOpenUserService;
import com.wlyuan.open.dto.OpenUserSyncDTO;
import com.wlyuan.open.infrastructure.exception.SourceValueErrorException;
import com.wlyuan.user.api.IUserService;
import lombok.Data;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import lombok.var;
import org.apache.dubbo.config.annotation.DubboReference;
import org.springframework.stereotype.Service;

/**
 * @author yuanzheng
 */
@Service
@Slf4j
@RequiredArgsConstructor
@Data
public class OpenUserServiceImpl implements IOpenUserService {
    @DubboReference(check = false)
    private final IUserService userService;

    @Override
    public Long sync(OpenUserSyncDTO source) {
        var creation = UserAssembler.assemble(source);
        var user = userService.rewrite(creation);
        logger.info("创建用户请求：{}", JSON.toJSON(user));
        AssertUtils.assertNotNull(user, new SourceValueErrorException("创建用户失败"), source);
        return user.getUserId();
    }
}
